<?php
	
	class Noticias extends Applib_Db_Table_Abstract
	{
		protected $_name = 'noticia';
        protected static $_instance = null;
		
        public static function getInstance()
        {
            if (null === self::$_instance) 
			{
                self::$_instance = new self();
            }
            return self::$_instance;
        }
		
		public static function getNoticias($id = null)
		{
			$table = self::getInstance();
            $select = $table->select();
			$select->setIntegrityCheck(false);
			$select->from($table);
			
			$select->joinLeft('usuario', 'usuario.id_user = noticia.id_user',
				array 
				(
					'user' 	=> 'usuario.user',
				)
			);
			
			if ($id)
			{
				$select->where('id_noticia = ?', $id);
			}
			else
			{
				$select->joinLeft
				(
					'comentario', 'comentario.id_noticia = noticia.id_noticia',
					array('num' =>  new Zend_Db_Expr('COUNT(comentario.id_noticia)'))
				);
				$select->where('noticia.activo=1');
				$select->order('noticia.id_noticia DESC');
				$select->limit(5);
			}
			
            return $table->fetchAll($select);
		}
	}